1 00:00:00,480 --> 00:00:01,560 Welcome back. 2 00:00:01,560 --> 00:00:07,770 In this lecture, we'll be creating another tool that heals our player when they've lost health. 3 00:00:07,770 --> 00:00:11,370 So when we click this tool as you can see we are healing ourself. 4 00:00:11,370 --> 00:00:14,520 And then bam we get health added back to our character. 5 00:00:14,520 --> 00:00:19,950 This tool will be very important for players when they're low on health and surrounded by zombies. 6 00:00:19,950 --> 00:00:22,020 So let's go ahead and get started. 7 00:00:22,660 --> 00:00:28,990 Inside server storage and inside of our tools folder, we have our med kit tool and we can go ahead 8 00:00:28,990 --> 00:00:33,910 and change the parent of this med kit tool to be the child of our rig. 9 00:00:33,910 --> 00:00:39,940 So that way you can see our rig has the med kit equipped, and then we can go ahead and create two animations 10 00:00:39,940 --> 00:00:41,170 for our med kit. 11 00:00:41,170 --> 00:00:45,820 We're going to need one animation for holding or having the med kit equipped. 12 00:00:45,820 --> 00:00:51,430 And then we're going to need another animation for us to be applying the med kit to ourselves, so we 13 00:00:51,430 --> 00:00:54,220 can go ahead and open up the animation editor again. 14 00:00:56,180 --> 00:01:00,050 And then we can select our rig and create our new animation. 15 00:01:00,050 --> 00:01:05,090 And again we'll want to have this animation last a total of one second across our timeline. 16 00:01:05,090 --> 00:01:11,540 And we're going to be able to adjust the speed of the animation from our script for the equipped animation. 17 00:01:11,540 --> 00:01:12,680 Mine is very simple. 18 00:01:12,680 --> 00:01:18,380 I just have my arms bent out in front of me, holding the med kit with both hands, and we'll make sure 19 00:01:18,380 --> 00:01:20,900 that we have this animation looped. 20 00:01:20,900 --> 00:01:23,990 And then we'll also make sure we have the priority set to movement. 21 00:01:23,990 --> 00:01:29,930 And then once you have your equipped animation completed, you can go ahead and publish it to the game 22 00:01:29,930 --> 00:01:33,200 and make sure you save that ID for later because we're going to need it. 23 00:01:33,200 --> 00:01:39,590 And then from this equip animation, you can go ahead and keep the exact same keyframe at the beginning 24 00:01:39,590 --> 00:01:40,760 as well as at the end. 25 00:01:40,760 --> 00:01:47,120 And then inside between those two keyframes, you can go ahead and start animating your healing animation. 26 00:01:48,610 --> 00:01:52,480 So here I have my healing animation and I'll go ahead and play it for you. 27 00:01:53,110 --> 00:01:56,740 As you can see, it's very fast because the timeline is only one second. 28 00:01:56,740 --> 00:02:01,120 But again, we're going to adjust the speed of this animation to stretch it out over a longer period 29 00:02:01,120 --> 00:02:01,870 of time. 30 00:02:01,870 --> 00:02:08,080 Just setting it to one second makes it easier for us to calculate the exact duration that we want for 31 00:02:08,080 --> 00:02:11,740 the animation, but as you can see, I've just customized it here a little bit. 32 00:02:11,740 --> 00:02:16,510 I've changed up the easing style and the easing direction, and I've just had my character, you know, 33 00:02:16,510 --> 00:02:21,340 move their arm around and their hand around to make it look like they're healing themselves. 34 00:02:21,340 --> 00:02:22,870 And that's the whole animation. 35 00:02:22,870 --> 00:02:28,390 Of course, you want to make sure that that kind of animation is not looped, and make sure your priority 36 00:02:28,390 --> 00:02:30,160 is set to action. 37 00:02:30,980 --> 00:02:33,620 And you can go ahead and publish it to Roblox. 38 00:02:33,620 --> 00:02:38,300 And then once you're done publishing it again, you can copy that animation ID we're going to need it 39 00:02:38,300 --> 00:02:39,170 for later. 40 00:02:39,590 --> 00:02:44,360 If you would like to use these animations that I've created again, I'll have this rig attached to the 41 00:02:44,360 --> 00:02:50,150 lecture that you can go ahead and grab, load the different animations on a rig and then publish them 42 00:02:50,150 --> 00:02:51,530 to Roblox. 43 00:02:52,720 --> 00:02:57,610 Now that we have our animations complete, we can go ahead and create a new module script inside of 44 00:02:57,610 --> 00:03:01,420 our med kit, and we're going to do the exact same formula that we did for our knife. 45 00:03:01,420 --> 00:03:04,300 We're going to call this module script our properties. 46 00:03:04,300 --> 00:03:07,030 And inside of here I'll have a table called self. 47 00:03:07,030 --> 00:03:10,750 And I'll make sure to return that at the end of my module script. 48 00:03:11,230 --> 00:03:15,670 And inside of this table is where we can store all of the different properties for our med kit. 49 00:03:15,670 --> 00:03:20,140 For example, one of those properties would be how much we would like to heal the player by. 50 00:03:20,140 --> 00:03:26,050 So we could call this healing amount, and we could set it to a value of like 50 health points. 51 00:03:26,650 --> 00:03:32,110 We can also define whether or not we would like this med kit to be a one time use med kit, or if the 52 00:03:32,110 --> 00:03:34,120 player is able to use it over and over again. 53 00:03:34,120 --> 00:03:35,500 So we could create a key. 54 00:03:35,500 --> 00:03:40,420 I'll call it one use and I'm going to set it for true in this instance. 55 00:03:40,420 --> 00:03:45,640 But we're going to script our med kit in a way where we can either let it be one use or not let it be 56 00:03:45,640 --> 00:03:46,240 one use. 57 00:03:46,240 --> 00:03:53,800 And if it's not going to be a one use med kit, then we can go ahead and define a cooldown for how long 58 00:03:53,800 --> 00:03:58,870 we should wait between allowing the player to heal because we don't want the player to be able to spam 59 00:03:58,870 --> 00:04:01,390 heal, so we can call this healing cooldown. 60 00:04:01,390 --> 00:04:06,310 You can set it to whatever value, but since I don't want my med kit to be one use, I'm just going 61 00:04:06,310 --> 00:04:08,320 to fill in some random value like one. 62 00:04:09,330 --> 00:04:11,730 And we can have a section for our animations. 63 00:04:11,760 --> 00:04:14,790 Again, we're going to want to have our equip animation. 64 00:04:14,790 --> 00:04:16,860 So we'll store the ID for that there. 65 00:04:16,860 --> 00:04:19,320 And then we also want to have our healing animation. 66 00:04:20,650 --> 00:04:25,720 And then down here we can also define the animation duration for our healing animation. 67 00:04:25,720 --> 00:04:28,510 And we could do something like maybe three seconds long. 68 00:04:29,660 --> 00:04:36,800 So make sure you've grabbed your animation IDs and pasted them into the correct, uh, key value pair. 69 00:04:37,280 --> 00:04:43,100 And now what we can do is we can go ahead and define the properties for the sound we would like to play 70 00:04:43,100 --> 00:04:44,360 to heal the player. 71 00:04:44,360 --> 00:04:47,270 So I'll call this healing sound Properties. 72 00:04:48,470 --> 00:04:53,360 And inside of here I'll also store the sound ID, so that way we don't have to store it in a separate 73 00:04:53,360 --> 00:04:54,200 property. 74 00:04:54,860 --> 00:05:02,030 So we'll have the sound ID for the sound we can have the roll off max distance. 75 00:05:02,030 --> 00:05:04,100 We'll set it to something like 35. 76 00:05:04,100 --> 00:05:06,770 We'll have the roll off minimum distance. 77 00:05:06,770 --> 00:05:08,450 That could be like 15 studs. 78 00:05:08,540 --> 00:05:13,790 The roll off mode will be the enum dot roll off mode dot inverse tapered. 79 00:05:14,710 --> 00:05:18,430 And then we could set the volume to like 0.25. 80 00:05:18,940 --> 00:05:24,400 Now, I did find a good healing sound ID from the toolbox right here, so if you would like to use that 81 00:05:24,400 --> 00:05:27,070 sound ID, you can go ahead and copy this number right here. 82 00:05:27,070 --> 00:05:30,250 And also place it inside of your Healing sound properties. 83 00:05:30,250 --> 00:05:32,830 And I'll go ahead and show you that sound as well. 84 00:05:32,830 --> 00:05:38,320 I'll just create a temporary sound instance here, and I'll put that sound ID inside of it. 85 00:05:38,530 --> 00:05:40,000 And then we can play the sound. 86 00:05:43,070 --> 00:05:44,510 Very basic. 87 00:05:44,960 --> 00:05:49,820 Now, as you can see, there is a little bit of a delay before the sound actually starts playing. 88 00:05:50,510 --> 00:05:55,970 Maybe about half a second or a little bit longer of a delay before we actually start hearing the sound. 89 00:05:56,000 --> 00:06:02,690 So what we can also do is inside of the healing sound properties, we can define the time position we 90 00:06:02,690 --> 00:06:05,660 would set for the sound before we start playing it. 91 00:06:05,870 --> 00:06:08,840 And we could set it to the value of like 0.8. 92 00:06:08,840 --> 00:06:13,250 So that way when we play the sound, it's going to start at 0.8 seconds, and we don't have to sit there 93 00:06:13,250 --> 00:06:18,260 and wait through that section of our sound that has no sound playing. 94 00:06:19,210 --> 00:06:27,100 So I'll go over and define another key inside of this table, and I'll just call it time position and 95 00:06:27,100 --> 00:06:30,640 set it to a value of like 0.8 seconds. 96 00:06:31,640 --> 00:06:35,870 Now that we have our module script set up, we can go ahead and add the local script as well as the 97 00:06:35,870 --> 00:06:39,560 server script that's going to be responsible for our med kit. 98 00:06:39,560 --> 00:06:45,710 And I'm going to show you a different method of scripting your tool without having to need to use a 99 00:06:45,710 --> 00:06:46,610 remote event. 100 00:06:46,670 --> 00:06:51,560 So what we're going to do for this one is we're going to have the local script, listen to the activated 101 00:06:51,560 --> 00:06:54,890 event and do its thing on the client side. 102 00:06:54,890 --> 00:06:59,960 And then we're also going to have the server script listen to the activated event and do its own thing 103 00:06:59,960 --> 00:07:01,040 on the server side. 104 00:07:01,040 --> 00:07:05,960 So that way we don't have to use a remote event to communicate between the local script and the server 105 00:07:05,960 --> 00:07:06,500 script. 106 00:07:06,500 --> 00:07:10,940 You can use a remote event, but I'm going to show you this method as well to show you there's multiple 107 00:07:10,940 --> 00:07:17,990 different ways we can approach solving a problem, such as trying to have two different scripts interact 108 00:07:17,990 --> 00:07:19,190 with the same tool. 109 00:07:20,120 --> 00:07:24,020 Inside of our local script, we're going to be using that exact same comment template that we've always 110 00:07:24,020 --> 00:07:24,860 been using. 111 00:07:25,620 --> 00:07:30,750 And we can go ahead and create several variables one to firstly reference the tool itself, which will 112 00:07:30,750 --> 00:07:32,100 be equal to script dot parent. 113 00:07:32,100 --> 00:07:35,340 And then we also want to get the properties for the tool. 114 00:07:35,340 --> 00:07:39,030 So we'll require tool dot properties. 115 00:07:39,690 --> 00:07:41,640 We'll also want to get our player. 116 00:07:41,640 --> 00:07:44,760 So that means we're going to have to grab the player service. 117 00:07:44,760 --> 00:07:47,850 So players is equal to game get service players. 118 00:07:47,850 --> 00:07:50,700 So player is going to be equal to players dot local player. 119 00:07:50,700 --> 00:07:54,780 And then we're going to need the player's character humanoid and animator. 120 00:07:54,780 --> 00:07:57,330 That way we can load the animations on our player. 121 00:07:57,330 --> 00:08:01,920 So our character is going to be equal to our player dot character. 122 00:08:01,920 --> 00:08:08,010 And just in case the character has not been loaded yet, we'll do or player dot character added and 123 00:08:08,010 --> 00:08:08,880 we'll wait. 124 00:08:09,980 --> 00:08:14,720 Then we can get the humanoid in our character, so that'll be equal to the character weight for child 125 00:08:14,720 --> 00:08:15,740 humanoid. 126 00:08:15,860 --> 00:08:17,990 And then we can also get the animator. 127 00:08:17,990 --> 00:08:20,780 So humanoid weight for child animator. 128 00:08:22,530 --> 00:08:29,310 And now basically what we can do is we can go back to server storage and go back to our knife tool and 129 00:08:29,310 --> 00:08:34,260 open up its local script, because I want to copy this code that we've done right here. 130 00:08:34,260 --> 00:08:39,150 I'm just going to copy this and then exit out and go back to my local script and paste that in there, 131 00:08:39,150 --> 00:08:41,700 because we're just going to be using the exact same function. 132 00:08:42,590 --> 00:08:47,570 Because now I can get my equip track and we'll just call Create Track. 133 00:08:47,570 --> 00:08:50,750 And that's going to be our properties dot equip animation. 134 00:08:51,420 --> 00:08:55,920 So now I can go ahead and listen for when my tool is equipped. 135 00:08:58,850 --> 00:09:00,950 I can also go ahead and listen for my tools. 136 00:09:00,950 --> 00:09:02,030 Unequipped. 137 00:09:03,520 --> 00:09:07,180 And then I can go ahead and listen for when my tool is activated. 138 00:09:08,730 --> 00:09:12,750 And then in the equip section we want to make sure to play the equip track. 139 00:09:13,570 --> 00:09:18,250 And then for this section, we want to make sure we stop playing the equip track when we don't have 140 00:09:18,250 --> 00:09:20,050 the tool equipped anymore. 141 00:09:20,990 --> 00:09:26,030 And then inside of the activated event, we can go ahead and create a variable. 142 00:09:26,030 --> 00:09:27,650 I'll call it Healing Track. 143 00:09:28,070 --> 00:09:31,280 And again we'll be using the Create Track function to create. 144 00:09:32,030 --> 00:09:38,270 A animation track from the healing animation, and then we can play our healing track. 145 00:09:38,270 --> 00:09:43,820 And then again, we're going to adjust the speed of our healing track to be equal to one divided by 146 00:09:43,820 --> 00:09:44,750 our properties. 147 00:09:44,750 --> 00:09:46,520 Dot animation duration. 148 00:09:47,220 --> 00:09:51,630 And then when we can go ahead and do is listen for when our healing track has ended. 149 00:09:52,420 --> 00:09:54,910 And we'll connect a function to that event. 150 00:09:54,910 --> 00:09:58,840 And this is where we'll just destroy our healing track. 151 00:09:59,380 --> 00:10:03,790 And this is where we would also perhaps set a debounce to be false. 152 00:10:03,790 --> 00:10:07,750 So that way we're not like spam playing our healing animation. 153 00:10:07,750 --> 00:10:10,060 So let's go ahead and create a variable for doing that. 154 00:10:10,060 --> 00:10:14,200 I'll call it the healing Debounce set equal to false. 155 00:10:15,090 --> 00:10:18,510 And then here we'll set healing debounce equal to true. 156 00:10:18,510 --> 00:10:20,760 And then we'll check if healing debounce is true. 157 00:10:20,760 --> 00:10:21,870 If it is, we'll return. 158 00:10:21,870 --> 00:10:24,720 That way we don't spam play the same healing animation. 159 00:10:24,720 --> 00:10:29,130 And then once our healing animation ends, we can set healing debounce back to false. 160 00:10:31,070 --> 00:10:38,660 But we only want to set the healing debounce back to false after we have waited for our healing cooldown. 161 00:10:38,660 --> 00:10:43,640 And that's just in case this med kit isn't a one time use med kit. 162 00:10:43,640 --> 00:10:52,310 So what we can do is we can have an if statement and check if we do not have the property of one, use 163 00:10:52,310 --> 00:10:53,330 set to true. 164 00:10:53,330 --> 00:11:00,020 So if this is a med kit that can be reused multiple times, then we can go ahead and add a yield statement 165 00:11:00,020 --> 00:11:04,130 and wait for that healing cooldown. 166 00:11:04,960 --> 00:11:08,620 And then we can go ahead and set the healing debounce back to false. 167 00:11:08,620 --> 00:11:13,750 Otherwise, if this med kit is a one time use, then we don't need to worry about healing debounce because 168 00:11:13,750 --> 00:11:18,010 this entire tool will be destroyed by our server script. 169 00:11:19,340 --> 00:11:24,740 So if we hop into our server script, we'll be using that exact same template as before. 170 00:11:27,320 --> 00:11:33,620 And inside of here we're basically going to just need the tool which is script dot parent as well as 171 00:11:33,620 --> 00:11:35,240 the properties for the tool. 172 00:11:35,240 --> 00:11:38,720 So that'll be require tool dot properties. 173 00:11:40,770 --> 00:11:44,820 And then we'll make sure to have a debounce on the server side as well. 174 00:11:45,210 --> 00:11:50,460 And what we're going to want to do is we want to listen to when our tool is activated. 175 00:11:52,560 --> 00:11:55,950 And here is where we'll set our healing balance equal to true. 176 00:11:55,950 --> 00:12:00,870 And then we'll make sure if our healing de balance is already true, we just return. 177 00:12:01,350 --> 00:12:07,260 Otherwise, we should play the healing sound and then wait for the player's animation to end. 178 00:12:07,260 --> 00:12:09,990 And then afterwards we can go ahead and heal them. 179 00:12:10,170 --> 00:12:17,460 So what we can do is we can actually go to our knife again, open up that server script, and we're 180 00:12:17,460 --> 00:12:23,250 basically just going to copy the exact same function in here and then just use it in our other script 181 00:12:23,250 --> 00:12:25,800 here because we need to create a sound. 182 00:12:25,830 --> 00:12:28,560 So I'll create a variable, I'll call this my healing sound. 183 00:12:28,980 --> 00:12:31,200 And it's going to be equal to create sound. 184 00:12:31,380 --> 00:12:37,290 But I do not need to pass the ID here because the ID is contained in our sound properties. 185 00:12:37,290 --> 00:12:38,700 So I'll just delete that. 186 00:12:39,420 --> 00:12:41,850 And then we can go ahead and create our sound. 187 00:12:41,880 --> 00:12:48,630 The parent will be we could do tool dot. 188 00:12:49,380 --> 00:12:50,220 Andl. 189 00:12:51,410 --> 00:12:56,270 And then the properties is going to be the healing sound properties. 190 00:12:57,520 --> 00:13:00,280 Then we can go ahead and play our healing sound. 191 00:13:00,280 --> 00:13:02,800 So healing sound play. 192 00:13:02,950 --> 00:13:09,880 And then when our healing sound has ended, we'll connect a function and we will destroy the healing 193 00:13:09,880 --> 00:13:10,600 sound. 194 00:13:12,620 --> 00:13:18,260 And after we play the healing sound and we can go ahead and wait for the player to stop playing their 195 00:13:18,260 --> 00:13:18,830 animation. 196 00:13:18,830 --> 00:13:20,990 So we'll just use the animation duration. 197 00:13:21,080 --> 00:13:26,840 This allows the server to keep a rough estimate of when the player's animation ends, and then what 198 00:13:26,840 --> 00:13:34,280 we can go ahead and do is we want to make sure that the player still has their tool equipped the entire 199 00:13:34,280 --> 00:13:34,970 time. 200 00:13:35,060 --> 00:13:40,580 So in this yield statement right here in those three seconds, the player could have unequipped their 201 00:13:40,580 --> 00:13:41,390 med kit. 202 00:13:41,390 --> 00:13:47,360 So if they do not have their med kit still equipped after this yield statement, then we don't want 203 00:13:47,360 --> 00:13:48,350 to heal that player. 204 00:13:49,130 --> 00:13:56,270 So what we can do is we can check if the tool dot parent is equal to, or we can check the name if it's 205 00:13:56,270 --> 00:13:59,000 equal to backpack. 206 00:13:59,000 --> 00:14:05,930 So if it's not equal to backpack which means our player should have it equipped, then we can do tool 207 00:14:05,930 --> 00:14:11,210 dot parent dot humanoid dot health is equal to. 208 00:14:11,210 --> 00:14:16,640 And we're just going to set it plus equal the properties dot healing amount. 209 00:14:18,250 --> 00:14:26,860 Otherwise, if the tool is inside of the backpack, then we'll just set healing debounce back to false 210 00:14:26,860 --> 00:14:28,450 and then we're going to return. 211 00:14:28,450 --> 00:14:33,700 We don't want to heal the player when they don't have it equipped, and then we need to check whether 212 00:14:33,700 --> 00:14:36,220 or not this med kit is a one time use. 213 00:14:36,220 --> 00:14:43,600 If it's not a one time use, then we're going to wait for our properties dot healing cooldown and then 214 00:14:43,600 --> 00:14:45,850 we can set healing Debounce back to false. 215 00:14:46,630 --> 00:14:50,560 Otherwise, if our med kit is a one time use, then we want to destroy it. 216 00:14:50,560 --> 00:14:54,220 So we'll use tool and we'll call the destroy function. 217 00:14:54,850 --> 00:14:59,560 And that's all we should need to do to create a healing med kit for our player. 218 00:14:59,560 --> 00:15:02,620 So let's go ahead and test what we've gotten done so far. 219 00:15:02,650 --> 00:15:07,990 We'll go ahead and grab our med kit and set its parent equal to our starter pack. 220 00:15:07,990 --> 00:15:09,760 And then we can go and playtest our game. 221 00:15:12,510 --> 00:15:13,830 And we'll click play. 222 00:15:14,980 --> 00:15:17,470 And if I hold my med kit, perfect. 223 00:15:17,470 --> 00:15:23,800 We have our animation plane and then let me go to the server and go in the workspace and let me go ahead 224 00:15:23,800 --> 00:15:25,450 and set my health to one. 225 00:15:26,100 --> 00:15:27,480 I'll go back to my player. 226 00:15:27,480 --> 00:15:28,920 So I have very little health. 227 00:15:28,920 --> 00:15:31,290 And then if I click, there we go. 228 00:15:31,290 --> 00:15:32,430 I'm healing up. 229 00:15:32,940 --> 00:15:33,990 And perfect. 230 00:15:33,990 --> 00:15:36,600 The server gave me my health back. 231 00:15:36,810 --> 00:15:41,730 Now, as you can see, there's an issue here where our animation is still playing. 232 00:15:41,730 --> 00:15:45,930 So that means what we're going to have to do is we're going to have to listen on the client side, when 233 00:15:45,930 --> 00:15:52,980 our tool is destroyed and when our tool is destroyed, then we can go ahead and stop playing our animation. 234 00:15:52,980 --> 00:15:54,810 Because this is an issue. 235 00:15:54,810 --> 00:15:58,710 We don't want to have our animation still playing on our player. 236 00:15:59,800 --> 00:16:05,290 Now, the simplest way we could do this is just refer to our tool and listen to the destroying event 237 00:16:05,290 --> 00:16:07,090 and connect a function to it. 238 00:16:07,800 --> 00:16:11,100 And here we could just stop the equip track. 239 00:16:11,250 --> 00:16:16,320 And then since we don't need the equip track anymore, we can go ahead and destroy the equip track. 240 00:16:17,020 --> 00:16:21,130 Now you think this code would work fine and you would be right. 241 00:16:21,130 --> 00:16:27,910 But actually, this code only works if the game is set to something called immediate signaling. 242 00:16:27,910 --> 00:16:31,540 So I'm going to quickly go over to my workspace. 243 00:16:31,990 --> 00:16:37,240 And inside of the properties panel you can see there is a setting right here called Signal Behavior. 244 00:16:37,240 --> 00:16:40,510 And right now it's set to a signal behavior called deferred. 245 00:16:40,540 --> 00:16:46,510 Now I'm not going to go into the topic and differences between immediate signaling and deferred signaling 246 00:16:46,510 --> 00:16:49,120 as that's too advanced to discuss right now. 247 00:16:49,120 --> 00:16:55,390 But just know that deferred events are going to be the future for the Roblox platform. 248 00:16:55,390 --> 00:16:59,770 All games that you create will be set to deferred by default. 249 00:16:59,800 --> 00:17:07,750 The issue with this is that it's going to break certain things in our scripts, such as trying to listen 250 00:17:07,750 --> 00:17:14,830 to this destroying event for our tool from inside a script that is also being destroyed at the same 251 00:17:14,830 --> 00:17:15,520 time. 252 00:17:15,520 --> 00:17:21,520 This code would work fine with immediate events, but with deferred events, which is going to be the 253 00:17:21,520 --> 00:17:26,170 default, we are going to have to do something a little bit different because this code is not going 254 00:17:26,170 --> 00:17:26,980 to work. 255 00:17:27,370 --> 00:17:34,480 Now, I've tried experimenting a little bit to try and figure out what would be the best solution for 256 00:17:34,480 --> 00:17:40,810 stopping an animations when the tool is being destroyed within this local script that is inside of our 257 00:17:40,810 --> 00:17:41,500 tool. 258 00:17:41,500 --> 00:17:47,410 And the best solution that works that I've found so far is we're going to have to use the task library 259 00:17:47,410 --> 00:17:49,000 and spawn a new thread. 260 00:17:49,900 --> 00:17:55,510 And inside of this thread is we're going to have to yield the thread again. 261 00:17:55,510 --> 00:17:57,730 So we're going to listen to the destroying event. 262 00:17:57,730 --> 00:18:02,980 But we're going to use the wait function and yield the current thread until the signal fires. 263 00:18:02,980 --> 00:18:07,810 And then once that signal fires, we can stop the clip track. 264 00:18:07,810 --> 00:18:11,380 And then we can go ahead and destroy the equip track. 265 00:18:11,380 --> 00:18:15,610 And this is what works with a deferred signaling enabled. 266 00:18:15,880 --> 00:18:21,670 Now I don't know if this was done on purpose or is simply a mistake or a byproduct of deferred signaling, 267 00:18:21,670 --> 00:18:25,750 so in the future they might fix it where this event will fire from the script. 268 00:18:25,750 --> 00:18:31,060 But as it stands currently, this is not going to work with deferred signaling. 269 00:18:31,060 --> 00:18:37,300 And since deferred signaling is the default for all games, now you're going to have to do it with this 270 00:18:37,300 --> 00:18:39,910 method and I'll prove that to you. 271 00:18:39,940 --> 00:18:44,920 Let me go ahead and set or let me keep signaling behavior set to deferred. 272 00:18:44,920 --> 00:18:47,530 And I'm going to comment this chunk of code out. 273 00:18:48,460 --> 00:18:50,680 And then we're going to playtest our game again. 274 00:18:53,410 --> 00:18:57,040 And let me just activate and use my tool. 275 00:18:58,230 --> 00:19:00,390 And you should see that my animation. 276 00:19:00,390 --> 00:19:03,300 As you can see, my animation is still stuck on my character. 277 00:19:04,070 --> 00:19:09,920 However, if I go and set the signal behavior to immediate. 278 00:19:10,820 --> 00:19:12,260 I'll playtest my game. 279 00:19:13,880 --> 00:19:18,380 And now we are getting another error because of immediate signaling. 280 00:19:18,380 --> 00:19:20,450 Man, I really love Roblox. 281 00:19:20,450 --> 00:19:24,350 Sometimes cannot load the animation clip provider service. 282 00:19:24,350 --> 00:19:26,600 Don't worry, that's not a problem in our code. 283 00:19:26,600 --> 00:19:35,390 This is a problem on Roblox end, and that's because it's giving us this error of cannot load the animation 284 00:19:35,390 --> 00:19:44,630 clip provider service from our create track function, because we can only use or load animations on 285 00:19:44,630 --> 00:19:47,570 a character when it's a descendant of the workspace. 286 00:19:47,570 --> 00:19:52,310 So you might be thinking, well, wait, we're grabbing the player's character right here, or we're 287 00:19:52,310 --> 00:19:53,870 waiting for it to be added. 288 00:19:54,140 --> 00:19:55,550 Why are we getting this error? 289 00:19:55,550 --> 00:19:58,520 Shouldn't our player be a descendant of the workspace? 290 00:19:58,520 --> 00:20:03,020 And no, it isn't a descendant of the workspace for some reason. 291 00:20:03,020 --> 00:20:08,990 So with immediate signaling, we're having this other issue of cannot load the animation clip provider 292 00:20:08,990 --> 00:20:09,620 service. 293 00:20:09,620 --> 00:20:15,680 And to fix that problem, another way you could do it is you could just refer to the workspace and then 294 00:20:15,680 --> 00:20:18,710 wait for our player. 295 00:20:19,720 --> 00:20:21,070 To be in the workspace. 296 00:20:21,070 --> 00:20:23,350 This is the other solution you'd have to do for this error. 297 00:20:23,350 --> 00:20:26,590 I know Roblox is being stupid sometimes. 298 00:20:26,590 --> 00:20:28,360 This is what we have to deal with. 299 00:20:28,660 --> 00:20:35,560 So in order for me to demonstrate immediate signal behavior, let me fix this problem and then we'll 300 00:20:35,560 --> 00:20:37,420 go back and playtest our game again. 301 00:20:39,310 --> 00:20:41,350 And hopefully we should have this problem fixed. 302 00:20:41,350 --> 00:20:41,920 Okay, good. 303 00:20:41,920 --> 00:20:43,030 The error is gone. 304 00:20:43,030 --> 00:20:43,300 Okay. 305 00:20:43,300 --> 00:20:44,290 There's our animation. 306 00:20:44,290 --> 00:20:46,330 If we use the med kit. 307 00:20:47,780 --> 00:20:48,320 Perfect. 308 00:20:48,320 --> 00:20:54,710 As you can see, the animation ended and we're back in our normal, uh, idling animation for our player's 309 00:20:54,710 --> 00:20:55,100 character. 310 00:20:55,100 --> 00:20:56,870 So that's with immediate signaling. 311 00:20:57,850 --> 00:21:02,110 But as you saw earlier with deferred signaling, it doesn't work. 312 00:21:02,110 --> 00:21:06,190 Now, you could be thinking, well, why don't we just set the game to immediate signaling and not worry 313 00:21:06,190 --> 00:21:06,550 about it? 314 00:21:06,550 --> 00:21:08,770 And you would be right in that regard. 315 00:21:08,770 --> 00:21:16,090 But since deferred signaling is being pushed as the new thing or the thing that's going to be preferred 316 00:21:16,090 --> 00:21:21,100 in the future, I have a feeling that immediate signaling is probably going to be deprecated or at some 317 00:21:21,100 --> 00:21:23,050 point in the future, removed. 318 00:21:23,050 --> 00:21:29,080 So it's always good to be using the new thing, which is deferred signaling if you want to set it to 319 00:21:29,080 --> 00:21:31,210 immediate and keep it out immediate, fine. 320 00:21:31,210 --> 00:21:37,570 But I'm going to keep it at deferred because all games by default are going to be set to the deferred 321 00:21:37,570 --> 00:21:38,740 signal behavior. 322 00:21:40,070 --> 00:21:46,280 So let me go ahead and delete this because we're back in the deferred mode, which fixes that problem. 323 00:21:46,940 --> 00:21:50,330 And then let me go ahead and show you the solution. 324 00:21:50,980 --> 00:21:53,890 For the deferred signal behavior. 325 00:21:53,890 --> 00:21:58,210 So this code right here is going to work with deferred signaling enabled. 326 00:21:58,390 --> 00:22:00,190 So if we go and play test our game. 327 00:22:01,590 --> 00:22:03,060 And we hop back in. 328 00:22:03,760 --> 00:22:05,680 Now if I go ahead and use my med kit. 329 00:22:08,170 --> 00:22:13,930 When we're done, perfect, the animation stops, so I don't know why that works the way it does. 330 00:22:13,960 --> 00:22:17,830 I'm not a Roblox engineer and I don't know what they do under the hood in their engine. 331 00:22:17,860 --> 00:22:25,240 Hopefully they fix this problem, but as it stands currently, this is the solution we have to employ 332 00:22:25,240 --> 00:22:28,000 with their new deferred signal behavior. 333 00:22:28,090 --> 00:22:32,470 I know we love Roblox so much when they make these changes. 334 00:22:33,150 --> 00:22:38,880 Now, one last thing I want to address in our code is that you've noticed that we are able to use our 335 00:22:38,880 --> 00:22:41,040 med kit even though we're at full health. 336 00:22:41,040 --> 00:22:46,290 So if you want to avoid the player from accidentally using their med kit when they have full health, 337 00:22:46,290 --> 00:22:49,500 that we can go back to our server script. 338 00:22:50,550 --> 00:22:58,200 And what we want to do is we want to make sure that our player's character's health is not equal to 339 00:22:58,200 --> 00:22:59,040 its max health. 340 00:22:59,040 --> 00:23:02,460 So if we've lost health, then we can go ahead and use the med kit. 341 00:23:02,850 --> 00:23:11,730 But let's say if our tool dot parent dot humanoid and the health if this is greater than or equal to 342 00:23:11,730 --> 00:23:18,840 our tool dot parent dot humanoid and the max health property, then we're just going to return and we 343 00:23:18,840 --> 00:23:23,220 can do this exact same thing on the client side as well to prevent the animation from playing. 344 00:23:23,220 --> 00:23:31,320 So if our humanoids health property that's greater than or equal to the humanoids Max health property, 345 00:23:31,320 --> 00:23:33,480 then we don't want to play the animation. 346 00:23:34,140 --> 00:23:36,030 So now if we go and play test. 347 00:23:36,840 --> 00:23:41,760 If I try to use my tool while I have full health, it shouldn't work. 348 00:23:41,760 --> 00:23:44,760 So if I click as you can see, nothing is happening. 349 00:23:44,760 --> 00:23:52,680 It isn't until I go to the server and I set my health to be very low, and then I go back that I am 350 00:23:52,680 --> 00:23:54,840 now able to use my med kit. 351 00:23:55,580 --> 00:23:59,090 And as you can see, and now I get health added back to my player's character. 352 00:23:59,090 --> 00:23:59,930 Very cool. 353 00:24:00,980 --> 00:24:01,850 Alrighty. 354 00:24:01,850 --> 00:24:05,660 And that is how you can create a tool that heals the player. 355 00:24:05,660 --> 00:24:10,730 Sorry about the issues we encountered earlier regarding deferred and immediate signaling. 356 00:24:10,760 --> 00:24:15,920 This is just the problems we will encounter when Roblox makes these kinds of changes. 357 00:24:15,920 --> 00:24:18,740 Otherwise, you're doing great so far on this project. 358 00:24:18,740 --> 00:24:21,680 Keep up the good work and I'll see you in the next lecture.